package com.joycessh.search.index.config;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

import java.net.URISyntaxException;


/**
 * @Author Joycessh
 * @Description
 * @Date 4/26/19 11:45 PM
 */
@Configuration
public class HbaseConfig {

    @Value("${hbase.zookeeper.quorum}")
    private String quorum = "localhost";

    @Value("${hbase.zookeeper.port}")
    private String port = "2181";

    @Bean
    public HbaseTemplate getHbaseTemplate() {
        HbaseTemplate hbaseTemplate = new HbaseTemplate();
//        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
//        conf.set("hbase.zookeeper.quorum", quorum);
//        conf.set("hbase.zookeeper.port", port);
        hbaseTemplate.setConfiguration(getConfiguration());
        hbaseTemplate.setAutoFlush(true);
        return hbaseTemplate;
    }


    private org.apache.hadoop.conf.Configuration getConfiguration() {
        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", quorum);
        configuration.set("hbase.zookeeper.port", port);
        return configuration;
    }

    /**
     * 拷贝HBase目录下的hbase-site.xml和Hadoop目录下的core-site.xml、hdfs-site.xml，
     * 将三个文件复制到src/main/resources目录下
     * @author Joycessh
     * @param
     * @return org.apache.hadoop.conf.Configuration
     * @date 4/27/19 6:26 PM
     */
    private org.apache.hadoop.conf.Configuration getConfigurationByResources()
    {
        try
        {
            org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
            configuration.addResource(new Path(ClassLoader.getSystemResource("hdfs-site.xml").toURI()));
            configuration.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
            configuration.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
            return configuration;
        }
        catch (URISyntaxException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
